nginx启动脚本和配置文件

  1. 编写 nginx 启动脚本,并加入系统服务
1
vim /etc/init.d/nginx

        写入内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL

        说明:该启动脚本来自互联网。

        保存后,更改权限:

1
2
chmod 755 /etc/init.d/nginx
chkconfig --add nginx

        如果需要开机启动,执行:

1
chkconfig nginx on
  1. 更改 nginx 配置

        首先把原来的配置文件清空

1
> /usr/local/nginx/conf/nginx.conf

        “>”这个符号为重定向的意思,单独用它,可以把一个文本文档快速清空。

1
vim /usr/local/nginx/conf/nginx.conf

        添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
include /usr/local/nginx/conf/vhosts/*.conf;
}

        说明:该配置文件可以作为一个模版,可以用虚拟机服务器上,工作中也可以参考

        nginx 的虚拟主机配置文件放在 /usr/local/nginx/conf/vhosts 下边,该部分配置改为include /usr/local/nginx/conf/vhosts/*.conf;

        保存配置后,先检验一下配置文件是否有错误存在:

1
/usr/local/nginx/sbin/nginx -t

        如果显示内容如下,则配置正确,否则需要根据错误提示修改配置文件:

1
2
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

        重启 nginx 服务

1
service nginx restart

        apache 有一个默认虚拟主机,也就是说无论什么域名只要指向到这台机器都会访问到这个虚拟主机。其实,在 nginx 里面也有一个这样的默认虚拟主机,但它有一个配置可以用来标记哪个虚拟主机是默认的。

1
2
3
mkdir /usr/local/nginx/conf/vhost
cd /usr/local/nginx/conf/vhost
vim defaule.conf

        加入配置:

1
2
3
4
5
6
7
8
server
{
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root /tmp/tmp;
deny all;
}

        说明:在之前的 nginx.conf 中就已经定义了 include 语句,意思是它会包含一些配置,在这里它会把 /usr/local/nginx/conf/vhosts/ 目录下的所有 *.conf 文件加载。所以,在这个目录下定义了一个 default.conf 文件,在这里会发现 listen 80 后面还有一个关键词叫做 “default_server”,这个就是用来标记它是默认虚拟主机的。使用 deny all 限制了该虚拟主机禁止被任何人访问。

        创建默认主机下定义的文件夹,不然会报错

1
mkdir /tmp/tmp

        如果有新的虚拟主机如 123.com 则编辑配置文件 123.conf

1
vim 123.conf

        加入内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server
{
listen 80;
server_name 123.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}

        查看监听端口

1
netstat -lnp

        监听的是 ip+prot 的形式,所以配置文件需要更改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server
{
listen 80;
server_name 123.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}